#include <stdio.h>
#include <string.h>

int main() {
  // c语言没有原生的字符串
  // 容纳9个字符的字符串 最后一个是\0
  char localString[10];
  // 双引号是字符
  char* a = "Hello";  
  // 单引号是单个字符,两者不能互换的
  int b = 'H';
  // char b = 'Hello'; 多个字符报错
  // 空格会合并
  char greeting[50] = "hello, ""how are you"" today!";
  // 等同于
  // char greeting[50] = "hello, how are you today!";
  // 合并新写法
  char greeting1[50] = "Hello,"
    "how are you "
    "today!";
  printf("%s\n", greeting1);

  // 两种声明方法
  char s1[14] = "Hello, world!";
  // 声明长度可以大于实际长度但是不能小于长度
  char s2[50] = "hello";
    // 长度可以省略
    // s3被编译器看是一个常量
  char* s3 = "Hello, world";
  // 第一个区别
  // s3[0] = 'z'; 不能修改,这是错误的,
  // 但是数组声明的是可以任意修改的
  s2[0] = 'z';
  printf("%s\n", s2);
  // 提醒不能修改可以加上const
  const char* s4 = "can't modify";
  // 第二个区别
  // 数组字符串不能修改本身的指向,因为c语言的数组不能修改指向
  // localString = "abc"; 
  // 想要复制只能使用拷贝赋值,注意不能原数组长
  strcpy(localString, "abc");
  printf("%s\n", localString);
  // 返回不包含\0的字符串的长度
  int len = strlen(localString);
  printf("%d\n", len);
  // 注意与sizeof的区别
  printf("%ld\n", sizeof(localString));
  // 多了个最大参数,防止边界溢出
  strncpy(localString, "123", strlen(localString));
  // 字符串拼接,改变第一个字符串,但是不会改变第二个
  strcat(localString, s2);
  // 注意第一个字符串必须要足够容纳第二个字符串
  printf("%s\n", localString);
  char s100[10] = "Monday";
  char s101[8] = "Tuesday";
  strncat(s100, s101, 3);
  puts(s100);

  // 字符串内容比较
  char* s200 = "Happy";
  char* s201 = "happy";
  char* s202 = "Happy New Year";
  printf("%i\n", strcmp(s200, s201));
  printf("%i\n", strcmp(s200, s202));
  printf("%i\n", strcmp("113", "114"));
  // 相等返回0
  printf("%i\n", strcmp("114", "114"));
  //比较指定位置的字符串
  printf("%i\n", strncmp("1112", "1113", 3));
  // 把数据写入字符串
  char first[6] = "hello";
  char last[6] = "world";
  char s300[40];
  sprintf(s300, "%s %s", first, last);
  printf("%s\n", s300);
  strcpy(s300, "");
  snprintf(s300, 40, "-%s %s-", first, last);
  printf("%s\n", s300);

  // 字符串数组
  // 7可以省略
  char weekdays[7][10] = {
    "Monday", 
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
  };
  // 改写成省空间的写法
  // 变成一维数组,全部变成字符指针
  char* weekdays1[] = {
    "Monday", 
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
  };
  // 遍历字符串数组
  for (int i = 0; i < 7; i++) {
    printf("%s\n", weekdays1[i]);
  }

	return 0;
}